<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      //工厂模式创建对象
      function creatObj(name, age) {
        //var Objcreat = function (name, age)
        var obj = new Object();
        obj.name = name;
        obj.age = age;
        obj.fn = function () {
          return this.name + this.age;
        };
        return obj;
      }
      //创建对象
      var obj3 = creatObj("张三", 20);
      var obj4 = creatObj("李四", 23);
      console.log(obj3, obj4);
      console.log(obj3.fn());

      console.log(typeof obj3, typeof obj4);
      //typeof用来获取数据类型，一般只用来获取基本数据类型的;而引用型（对象，数组）返回的都是object。
      //instanceof:用来测试引用类型的实例属于哪一个类
      console.log(obj3 instanceof Object, obj4 instanceof Object);
      //无法判断obj3 和obj4具体属于哪个对象

      var x = 100;
      var y = "abc";
      console.log(typeof x, typeof y);
      var obj5 = {
        name: "fvvd",
        price: 20,
      };
      var arr = [1, 2, 3];
      var fn = function () {
        console.log("111");
      };
      console.log(typeof obj5, typeof arr, typeof fn);
      console.log(arr instanceof Array);
      console.log(arr instanceof Object);
      //所有的引用类型都属于Object的实例

      //构造函数(类)创建对象
      function Person(name, age = 16) {
        //age = 16 默认初始值
        //构造函数名首字母名要大写(这里创建的不止是一个函数，实际上是构建了一个类)
        this.name = name; //this代表的不是Person而是实例化后的对象
        this.age = age;
        this.fn = function () {
          return this.name + this.age;
        };
      }
      //实例化对象
      var obj7 = new Person("王麻子", 77); //实列化时this指向该对象obj7
      var obj8 = new Person("武大郎", 66); //实列化时this指向该对象obj8
      console.log(obj7.name);
      console.log(obj7.fn());

      console.log(obj7 instanceof Object);
      console.log(obj8 instanceof Person);

      /*    //构造函数(类)创建对象
    function Student(name, age, grade) {
      this.name = name;
      this.age = age;
      this.grade = grade;
      this.fn1 = function (evaluate) {
        console.log(this.name + this.age + this.grade + evaluate);
      };
    }
    //创建对象
    var stu1 = new Student("张三", 18, "大一");
    var stu2 = new Student("李四", 20, "大三");
    stu1.fn1("是个好孩子");*/
    </script>
  </body>
</html>
